package cn.alpha.intell.rbtlib.wifi.dlalog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.rbt.utils.RBTKeyUtil;


public class OtaLowBatteryDialog extends Dialog {
    public Builder builder;

    public interface TimeFinish{
        public void finish();
    }

    public OtaLowBatteryDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public OtaLowBatteryDialog(Context context) {
        super(context);
    }



    private void applyCompat() {
        if (Build.VERSION.SDK_INT < 19) {
            return;
        }
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        applyCompat();
        super.onCreate(savedInstanceState);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String content;
        private String cancelText;
        private String networkText;
        private OnClickListener cancelClickListener;
        private OnClickListener networdClickListener;
        private int countTime;
        public TimeFinish timeFinish;
        private Button konwBtn;
        private ImageView closeImage;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * set the dialog title from String
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTimeFinishListener(TimeFinish timeFinishListener){
            this.timeFinish = timeFinishListener;
            return this;
        }

        public void setCountTime(int countTime){
            this.countTime = countTime;
           /* if (sureTv!=null){
                sureTv.setText(networkText+"(5s）");
            }*/
        }

        /**
         * set the dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog content from String
         *
         * @param content
         * @return
         */
        public Builder setMessage(String content) {
            this.content = content;
            return this;
        }

        /**
         * Set the Dialog content from resource
         *
         * @param content
         * @return
         */
        public Builder setMessage(int content) {
            this.content = (String) context.getText(content);
            return this;
        }

        /**
         * Set the negative button String and it's listener
         *
         * @param cancelText
         * @param cancelClickListener
         * @return
         */
        public Builder setCancelText(String cancelText, OnClickListener cancelClickListener) {
            this.cancelText = cancelText;
            this.cancelClickListener = cancelClickListener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param cancelText
         * @param cancelClickListener
         * @return
         */
        public Builder setCancelText(int cancelText, OnClickListener cancelClickListener) {
            this.cancelText = (String) context.getText(cancelText);
            this.cancelClickListener = cancelClickListener;
            return this;
        }

        /**
         * Set the positive button String and it's listener
         *
         * @param networkText
         * @param networdClickListener
         * @return
         */
        public Builder setNeworkText(String networkText, OnClickListener networdClickListener) {
            this.networkText = networkText;
            this.networdClickListener = networdClickListener;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param networkText
         * @param networdClickListener
         * @return
         */
        public Builder setNeworkText(int networkText, OnClickListener networdClickListener) {
            this.networkText = (String) context.getText(networkText);
            this.networdClickListener = networdClickListener;
            return this;
        }

        public OtaLowBatteryDialog create() {
           // final OtaLowBatteryDialog dialog = new OtaLowBatteryDialog(context, R.style.Dialog);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final OtaLowBatteryDialog dialog = new OtaLowBatteryDialog(context, R.style.Dialog);
            View view = inflater.inflate(R.layout.dialog_low_battery, null);
            konwBtn=(Button)view.findViewById(R.id.dialog_low_btn);
            closeImage=(ImageView)view.findViewById(R.id.closeImage);
            dialog.addContentView(view, new ViewGroup.LayoutParams(800, ViewGroup.LayoutParams.WRAP_CONTENT));
            konwBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            closeImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });


            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    // 恢复Voice,Home键
                    RBTKeyUtil.enableKeyHome(true);
                    RBTKeyUtil.enableKeyVoice(true);
                }
            });

            return dialog;
        }
    }

    @Override
    public void show() {
        // 屏蔽Voice,Home键
        RBTKeyUtil.enableKeyHome(false);
        RBTKeyUtil.enableKeyVoice(false);
        super.show();
    }
}
